{
    //观察模式
    interface AbstractSubject {
        registerObserver(observer: Observer): void
        remove(observer: Observer): void
        notifyObservers(): void
    }

    interface Observer {
        updated(): void
    }

    class SpecificSubject implements AbstractSubject {
        private observers: Array<Observer>

        constructor() {
            this.observers = new Array<Observer>
        }

        registerObserver(observer: Observer): void {
            this.observers.push(observer)
        }

        remove(observer: Observer): void {
            const observerIndex: number = this.observers.findIndex((listValue) => {
                return listValue === observer
            })

            observerIndex >= 0 && this.observers.splice(observerIndex, 1)
        }

        notifyObservers(): void {
            this.observers.forEach((observer) => {
                observer.updated()
            })
        }
    }

    class ConcreateObserver1 implements Observer {
        updated(): void {
            console.log("更新observer1");
        }
    }

    class ConcreateObserver2 implements Observer {
        updated(): void {
            console.log("更新observer2");
        }
    }

    const ObserverTest = (): void => {
        const subject: SpecificSubject = new SpecificSubject()
        const observer1: ConcreateObserver1 = new ConcreateObserver1()
        const observer2: ConcreateObserver1 = new ConcreateObserver2()

        subject.registerObserver(observer1)
        subject.registerObserver(observer2)
        subject.notifyObservers()
    }

    // ObserverTest()
}

{
    //发布订阅者模式
    interface Publish {
        registerObserver(eventType: string, subscribe: Subscribe): void
        remove(eventType: string, subscribe?: Subscribe): void
        notifyObserver(eventType: string, ...args: any): void
    }

    class ConcreatePublish implements Publish {
        private subscribes: any
        constructor() {
            this.subscribes = {}
        }
        registerObserver(eventType: string, subscribe: Subscribe): void {
            this.subscribes[eventType] = this.subscribes[eventType] ?? []
            this.subscribes[eventType].push(subscribe)
        }
        remove(eventType: string, subscribe?: any): void {
            const subscribeArray = this.subscribes[eventType]
            if (subscribeArray) {
                if (!subscribe) {
                    delete this.subscribes[eventType]
                } else {
                    this.subscribes.map((item: Subscribe, index: number) => {
                        if (subscribe === item) {
                            subscribeArray.splice(index, 1)
                        }
                    })
                }
            }
        }
        notifyObserver(eventType: string, ...args: any): void {
            const subscribes = this.subscribes[eventType]
            if (subscribes) {
                subscribes.forEach((item: Subscribe) => item.update(...args))
            }
        }
    }

    interface Subscribe {
        update(...value: any[]): void
    }

    class ConcreateSubscribe1 implements Subscribe {
        public update(...value: any[]): void {
            console.log("ConcreateSubscribe1 执行更新操作：", ...value);
        }
    }

    class ConcreateSubscribe2 implements Subscribe {
        public update(...value: any[]): void {
            console.log("ConcreateSubscribe2 执行更新操作：", ...value);
        }
    }

    const PublishTest = (): void => {
        const publish = new ConcreatePublish()
        const subscribe1 = new ConcreateSubscribe1()
        const subscribe2 = new ConcreateSubscribe2()

   
        publish.registerObserver("subscribe1", subscribe1)
        publish.registerObserver("subscribe2", subscribe2)
        console.log("原始发布者",publish);
        publish.notifyObserver("subscribe1", "更新值")
        publish.notifyObserver("subscribe2", "更新值")
    
        publish.remove("subscribe2")
        console.log("删除subscribe2后的发布者",publish);

    }

    PublishTest()
}